<template>

    <a-row
        :gutter="8"
        :wrap="false">
        <a-col flex="auto">
            <a-card type="flex">
                <a-table
                    :columns="columns"
                    :data-source="listData"
                    :loading="loading"
                    :pagination="paginationState"
                    :scroll="{ x: 1000 }"
                    @change="onTableChange">
                    <template #bodyCell="{ column, record }">
                        <template v-if="'account_number' === column.key">
                            <a @click="handleIpClick(record.id)">{{ record.account_number }}</a>
                        </template>

                    </template>
                </a-table>
            </a-card>
        </a-col>
    </a-row>

    <a-modal
        v-model:visible="isModalVisible"
        title="详细信息"
        @cancel="handleCancel"
        @ok="handleOk"
        width="60%">
        <a-table
            :columns="modalColumns"
            :data-source="modalData"
            :pagination="false"
            :scroll="{ y: 300 }" />
    </a-modal>

    <a-modal
        v-model:visible="isModalVisible2"
        title="详细信息"
        @cancel="handleCancel2"
        @ok="handleOk2"
        width="60%">
        <a-table
            :columns="modalColumns2"
            :data-source="modalData2"
            :pagination="false"
            :scroll="{ y: 300 }" />
    </a-modal>
</template>

<script setup>
import apis from '@/apis'
import { formatUtcDateTime } from '@/utils/util'
import { config } from '@/config'
import { usePagination } from '@/hooks'
import { onMounted, ref } from 'vue'
import { getCustomer, getFriend } from '@/apis/modules/user'

const columns = [
    { title: 'id', dataIndex: 'id', key: 'id' },
    { title: '账号id', dataIndex: 'account_number', key: 'account_number' },
    { title: '昵称', dataIndex: 'nickname', key: 'nickname' },
    { title: '手机号', dataIndex: 'phone', key: 'phone' },
    { title: '国家', dataIndex: 'country', key: 'country' },
    { title: '省', dataIndex: 'province', key: 'province' },
    { title: '市', dataIndex: 'city', key: 'city' },

]
const phone = ref()
const { listData, loading, showLoading, hideLoading, paginationState, resetPagination, searchFormData } =
    usePagination()
const isModalVisible2 = ref(false)
const isModalVisible = ref(false)
const modalData = ref([]) // Modal 显示的数据
const modalData2= ref([]) // Modal 显示的数据

const modalColumns = [

    { title: 'id', dataIndex: 'id', key: 'id' },
    { title: '用户名', dataIndex: 'name', key: 'name' },
]
const modalColumns2 = [
    { title: '消息', dataIndex: 'msg', key: 'msg' },
]
onMounted(() => {
    // 清理登录信息
    getPageList()
})
/**
 * 获取用户列表
 * @returns {Promise<void>}
 */
async function getPageList() {
    showLoading()
    const data = await apis.user.getCustomer()

    hideLoading()
    console.log('data')
        console.log(data)
        listData.value = data.data

    // if (config('http.code.success') === success) {
    //     console.log('data')
    //     console.log(data)
    //     listData.value = data
    // }
}
/**
 * 分页
 */
function onTableChange({ current, pageSize }) {
    paginationState.current = current
    paginationState.pageSize = pageSize
    getPageList()
}

/**
 * 搜索
 */
function handleSearch() {
    resetPagination()
    getPageList()
}
/**
 * 重置
 */
async function handleIpClick(id) {
    console.log('id clicked:', id)
    try {
        showLoading()
        // 请求 IP 相关数据
        const { success, data } = await apis.user.getFriend({
            from_account: id+"",
            start_index: 0,
        })
        hideLoading()
        if (config('http.code.success') === success) {
            console.log('data')
            console.log(data)
            modalData.value = data
            isModalVisible.value = true
        }
    } catch (error) {
        hideLoading()
    }
}

// 点击 设备ID
async function handleDeviceIdClick(deviceId) {
    console.log('Device ID clicked:', deviceId)
    try {
        showLoading()
        // 请求设备ID相关数据
        const { success, data } = await apis.users.getUsersInfoListByIpOrMac({ deviceId: deviceId })
        hideLoading()
        if (config('http.code.success') === success) {
            modalData.value = data
            isModalVisible.value = true
        }
    } catch (error) {
        hideLoading()
    }
}

// 关闭弹窗
function handleCancel() {
    isModalVisible.value = false
}

// 确认
function handleOk() {
    isModalVisible.value = false
}
function handleCancel2() {
    isModalVisible2.value = false
}

// 确认
function handleOk2() {
    isModalVisible2.value = false
}
</script>

<style lang="less" scoped></style>
